<template>
  <div>
    <!-- <template class="c-field" v-if="display">{{ display }}</template>
    <template v-else>
      <span class="c-field-placeholder">{{ placeholder }}</span>
    </template> -->
    <van-popup
      v-model="visible"
      position="bottom"
      get-container="body"
      class="c-mnhldr"
    >
      <div class="c-mnhldr-search flex-row flex-align-center">
        <van-field v-model="search" name="search" placeholder="请输入姓名">
          <template #left-icon>
            <!-- <img src="../../img/search.png" class="c-mnhldr-search-img" /> -->
          </template>
        </van-field>
        <div class="c-mnhldr-search-btn">搜索</div>
      </div>
      <div v-if="visible">
        <div v-for="item in list" :key="item.index">
          <div class="c-mnhldr-item-letter">{{ item.letter }}</div>
          <div
            v-for="itemData in item.data"
            :key="itemData.id"
            @click="handleClick(itemData)"
            class="c-mnhldr-item"
            :class="itemData.label == value ? 'c-mnhldr-item-active' : ''"
          >
            <div class="c-mnhldr-item-text">
              {{ itemData.label }}
            </div>
            <div class="c-mnhldr-item-number">
              身份证号 {{ itemData.value }}
            </div>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import { chineseLetter } from '@/utils/public/ch-sort.js'
export default {
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    columns: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    display() {
      const item = this.columns.find(item => item.label === this.value)
      return item ? item.label : ''
    },
    list() {
      if (this.columns.length === 0) {
        return []
      } else {
        let result = JSON.parse(JSON.stringify(this.columns))
        let data =
          this.search === ''
            ? result
            : result.filter(item => {
                return item.label.indexOf(this.search) > -1
              })
        // console.log('dfdfdfd', chineseLetter(result, 'label'), data)
        return data.length > 0 ? chineseLetter(data, 'label') : []
      }
    }
  },
  data() {
    return {
      visible: false,
      value: '',
      search: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.value = this.defaultValue
    },
    handleClick(item) {
      this.$emit('confirm', item)
      this.value = item.label
      this.visible = false
      this.search = ''
    }
  }
}
</script>
<style lang="less" scoped>
.c-field {
  font-size: 15px;
  color: #333;
}
.c-field-placeholder {
  color: #999;
  font-size: 15px;
}
.c-mnhldr {
  height: 449px;
  border-radius: 13px 13px 0 0;
  padding: 15px;
  box-sizing: border-box;
  &-search {
    .van-cell {
      padding-top: 4.5px;
      padding-bottom: 5.5px;
      height: 32px;
      border-radius: 17px 0 0 17px;
      border: 1px solid @c-color-primary;
    }
    &-img {
      width: 16px;
      height: 17px;
    }
    &-btn {
      width: 56px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      font-size: 13px;
      color: #fff;
      border-radius: 0 17px 17px 0;
      background: @c-color-primary;
    }
  }
  &-item {
    font-size: 15px;
    color: #666;
    margin-top: 15px;
    padding-bottom: 10px;
    border-bottom: 0.5px solid #e9e9e9;
    &:last-child {
      border-bottom: none;
    }
    &-letter {
      color: #999;
      font-size: 13px;
      // padding-left: 10px;
      background: #f3f3f3;
    }
    &-text {
      margin-top: 15px;
      font-size: 15px;
      color: #666;
    }
    &-number {
      margin-top: 8px;
      font-size: 13px;
      color: #999;
    }
  }
  &-item-active {
    color: @c-color-primary;
    .c-mnhldr-item-text,
    .c-mnhldr-item-number {
      color: @c-color-primary;
    }
  }
}
</style>
